package ruifight.member.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import ruifight.member.model.NoticeInfo;
import ruifight.member.service.NoticeInfoService;


@Controller
@RequestMapping("/interface")
public class NoticeInfoController {

	private static final Logger logger = LoggerFactory.getLogger(NoticeInfoController.class);

	@Autowired
	public NoticeInfoService noticeInfoService;

	/**
	 * 获取通知列表list
	 * @return
	 */
	@RequestMapping("/getNoticeInfoList")
	@ResponseBody
	public Map<String, Object> getNoticeInfoList(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		String cur_page= paramMap.get("cur_page").toString();
		String page_size= paramMap.get("page_size").toString();
		PageInfo<Map<String, Object>> pageInfo = noticeInfoService.getNoticeInfoList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;
	}

	/**
	 * 添加通知
	 * @param noticeInfo
	 * @return
	 */
	@RequestMapping("/addNoticeInfo")
	@ResponseBody
	public  Map<String, Object> addNoticeInfo(@RequestBody NoticeInfo noticeInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			noticeInfo.setCreateDate(new Date());
			noticeInfoService.insertSelective(noticeInfo);
			map.put("state", 1);
			map.put("msg", "添加成功");
		} catch (Exception e) {
			map.put("state", 0);
			map.put("msg", "添加失败");
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 修改通知
	 * @param noticeInfo
	 * @return
	 */
	@RequestMapping("/updateNoticeInfo")
	@ResponseBody
	public  Map<String, Object> updateNoticeInfo(@RequestBody NoticeInfo noticeInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			noticeInfoService.updateByPrimaryKeySelective(noticeInfo);
			map.put("state", 1);
			map.put("msg", "修改成功");
		} catch (Exception e) {
			map.put("state", 0);
			map.put("msg", "修改失败");
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 删除通知
	 * @param noticeInfo
	 * @return
	 */
	@RequestMapping("/delNoticeInfo")
	@ResponseBody
	public Map<String, Object> delNoticeInfo(@RequestBody NoticeInfo noticeInfo){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			noticeInfoService.deleteByPrimaryKey(noticeInfo.getNoticeInfoId());
			map.put("state",1);
			map.put("msg","删除成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","删除失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 获取最后一条通知
	 * @return
	 */
	@RequestMapping("/getLastNoticeInfo")
	@ResponseBody
	public NoticeInfo getLastNoticeInfo() {
		NoticeInfo noticeInfo= noticeInfoService.getLastNoticeInfo();
		return noticeInfo;
	}
	/**
	 * 获取通知
	 * @return
	 */
	@RequestMapping("/getNoticeInfo/{id}")
	@ResponseBody
	public NoticeInfo getNoticeInfo(@PathVariable Long id) {
		NoticeInfo noticeInfo= noticeInfoService.selectByPrimaryKey(id);
		return noticeInfo;
	}
}
